Add tests for empty list models
authorMatthias Clasen <mclasen@redhat.com>
Fri, 11 Jun 2021 19:38:20 +0000 (15:38 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 11 Jun 2021 19:38:20 +0000 (15:38 -0400)
Test that all our models return NULL for out-of-range
get_item calls, as expected.

testsuite/gtk/filterlistmodel.c
testsuite/gtk/multiselection.c
testsuite/gtk/singleselection.c

index fbcae6b3ad79e073b2e2b4a30c4f196b67fb3edb..cd109c2e0b22840852add5c6445d1f0ec60f280c 100644 (file)
@@ -376,6 +376,35 @@ test_incremental (void)
   g_object_unref (filter);
 }
 
+static void
+test_empty (void)
+{
+  GtkFilterListModel *filter;
+  GListStore *store;
+  GtkFilter *f;
+
+  filter = gtk_filter_list_model_new (NULL, NULL);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (filter)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (filter), 11));
+
+  store = g_list_store_new (G_TYPE_OBJECT);
+  gtk_filter_list_model_set_model (filter, G_LIST_MODEL (store));
+  g_object_unref (store);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (filter)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (filter), 11));
+
+  f = GTK_FILTER (gtk_every_filter_new ());
+  gtk_filter_list_model_set_filter (filter, f);
+  g_object_unref (f);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (filter)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (filter), 11));
+
+  g_object_unref (filter);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -389,6 +418,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/filterlistmodel/empty_set_filter", test_empty_set_filter);
   g_test_add_func ("/filterlistmodel/change_filter", test_change_filter);
   g_test_add_func ("/filterlistmodel/incremental", test_incremental);
+  g_test_add_func ("/filterlistmodel/empty", test_empty);
 
   return g_test_run ();
 }
index bdc896073b75b30d89ad23c2e8f8b3f790ce8ad8..7117efb4e77639841ca9eac2e1ec85db8568f2ee 100644 (file)
@@ -681,6 +681,47 @@ test_set_model (void)
   g_object_unref (selection);
 }
 
+static void
+test_empty (void)
+{
+  GtkMultiSelection *selection;
+  GListStore *store;
+
+  selection = gtk_multi_selection_new (NULL);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (selection)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (selection), 11));
+
+  store = g_list_store_new (G_TYPE_OBJECT);
+  gtk_multi_selection_set_model (selection, G_LIST_MODEL (store));
+  g_object_unref (store);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (selection)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (selection), 11));
+
+  g_object_unref (selection);
+}
+
+static void
+test_empty_filter (void)
+{
+  GtkStringList *stringlist;
+  GtkMultiSelection *selection;
+  GtkSelectionFilterModel *selection_filter;
+
+  stringlist = gtk_string_list_new (NULL);
+  gtk_string_list_append (stringlist, "first item");
+
+  selection = gtk_multi_selection_new (G_LIST_MODEL (stringlist));
+  selection_filter = gtk_selection_filter_model_new (GTK_SELECTION_MODEL (selection));
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (selection_filter)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (selection_filter), 11));
+
+  g_object_unref (selection_filter);
+  g_object_unref (selection);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -703,6 +744,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/multiselection/set_selection", test_set_selection);
   g_test_add_func ("/multiselection/selection-filter", test_selection_filter);
   g_test_add_func ("/multiselection/set-model", test_set_model);
+  g_test_add_func ("/multiselection/empty", test_empty);
+  g_test_add_func ("/multiselection/selection-filter/empty", test_empty_filter);
 
   return g_test_run ();
 }
index 88283c36fc98ed2777faf34f576e7bc563ae6d0b..bb6462cd97b206ad51da9e57cc3cfabf630bba2f 100644 (file)
@@ -706,6 +706,27 @@ test_set_model (void)
   g_object_unref (selection);
 }
 
+static void
+test_empty (void)
+{
+  GtkSingleSelection *selection;
+  GListStore *store;
+
+  selection = gtk_single_selection_new (NULL);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (selection)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (selection), 11));
+
+  store = g_list_store_new (G_TYPE_OBJECT);
+  gtk_single_selection_set_model (selection, G_LIST_MODEL (store));
+  g_object_unref (store);
+
+  g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (selection)), ==, 0);
+  g_assert_null (g_list_model_get_item (G_LIST_MODEL (selection), 11));
+
+  g_object_unref (selection);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -726,6 +747,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/singleselection/query-range", test_query_range);
   g_test_add_func ("/singleselection/changes", test_changes);
   g_test_add_func ("/singleselection/set-model", test_set_model);
+  g_test_add_func ("/singleselection/empty", test_empty);
 
   return g_test_run ();
 }